/**
 * Created on 2007-03-09 21:02
 */
package game;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;

import tools.Randomizer;

import manager.Manager;

/**
 * @author David Wallin
 */
public class ShapeManager extends Manager
{
	private enum ShapeDirections
	{
		LEFT,
		RIGHT,
		CENTER
	}
	
	public static final ShapeManager					INSTANCE = new ShapeManager();
	public static final int								MAXIMUM_NUMBER_OF_REMOVABLE_LINES = 3;
	private final HashMap<ShapeDirections, Color>		SHAPE_DIRECTION_COLORS;
	
	private Randomizer									m_Randomizer = Randomizer.INSTANCE;
	private ArrayList<BasicMatris>						m_Shapes;
	private HashMap<BasicMatris, Color>					m_ShapeColors;
	
	public BasicMatris getNextShape()
	{
		BasicMatris originalShape = m_Shapes.get(m_Randomizer.getNextInt(0, m_Shapes.size() - 1));
		
		int width = originalShape.getWidth();
		int height = originalShape.getHeight();
		BasicMatris shape = new BasicMatris(width, height);
		shape.setColor(m_ShapeColors.get(originalShape));
		
		for(int y = 0; y < height; y++)
		{
			for(int x = 0; x < width; x++)
			{
				shape.setPosition(x, y, originalShape.getPosition(x, y));
			}
		}
		
		return shape;
	}
	
	protected void doInitialize()
	{
		BasicMatris shape = new BasicMatris(4, 2);
		shape.setPosition(0, 0, true);		//this matris looks like this:
		shape.setPosition(1, 0, true);
		shape.setPosition(2, 0, true);		//,,,,
		shape.setPosition(3, 0, true);		//  '
		shape.setPosition(2, 1, true);
		addShape(shape, ShapeDirections.LEFT);
		
		shape = new BasicMatris(4, 2);
		shape.setPosition(0, 0, true);		//this matris looks like this:
		shape.setPosition(1, 0, true);
		shape.setPosition(2, 0, true);		//,,,,
		shape.setPosition(3, 0, true);		// '
		shape.setPosition(1, 1, true);
		addShape(shape, ShapeDirections.RIGHT);
		
		shape = new BasicMatris(3, 1);		//this matris looks like this:
		shape.setPosition(0, 0, true);
		shape.setPosition(1, 0, true);		//,,,
		shape.setPosition(2, 0, true);
		addShape(shape, ShapeDirections.CENTER);
		
		shape = new BasicMatris(3, 2);		//this matris looks like this:
		shape.setPosition(0, 0, true);
		shape.setPosition(1, 0, true);		//,,
		shape.setPosition(1, 1, true);		// ,,
		shape.setPosition(2, 1, true);
		addShape(shape, ShapeDirections.LEFT);
		
		shape = new BasicMatris(3, 2);		//this matris looks like this:
		shape.setPosition(1, 0, true);
		shape.setPosition(2, 0, true);		// ,,
		shape.setPosition(0, 1, true);		//,,
		shape.setPosition(1, 1, true);
		addShape(shape, ShapeDirections.RIGHT);
	}
	
	private void addShape(BasicMatris shape, ShapeDirections direction)
	{
		m_Shapes.add(shape);
		m_ShapeColors.put(shape, SHAPE_DIRECTION_COLORS.get(direction));
	}
	
	private ShapeManager()
	{
		SHAPE_DIRECTION_COLORS = new HashMap<ShapeDirections, Color>();
		SHAPE_DIRECTION_COLORS.put(ShapeDirections.LEFT, Color.blue);
		SHAPE_DIRECTION_COLORS.put(ShapeDirections.RIGHT, Color.red);
		SHAPE_DIRECTION_COLORS.put(ShapeDirections.CENTER, Color.yellow);
		
		m_Shapes = new ArrayList<BasicMatris>();
		m_ShapeColors = new HashMap<BasicMatris, Color>();
	}
}
